package com.cheegu.common.parser.gpsdata.reader.raw;

import com.cheegu.common.parser.gpsdata.RawDataReader;
import com.cheegu.common.parser.gpsdata.util.PathUtil;

import java.io.*;
import java.util.function.Consumer;

public abstract class PathRawDataReader implements RawDataReader {
    private String path;

    public PathRawDataReader(String path) {
        try {
            this.path = PathUtil.getAbsolutePath(path);
        } catch (FileNotFoundException e) {
            throw new RuntimeException("error path");
        }
    }

    protected File getFile() {
        return new File(path);
    }

    protected void readFile(Consumer<String> consumer) {
        File file = getFile();

        if (file.canRead()) {
            try {
                BufferedReader reader = new BufferedReader(new FileReader(file));
                while (reader.ready()) {
                    consumer.accept(reader.readLine());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            throw new RuntimeException("unable to read file: " + file.getPath());
        }
    }
}
